import NameImgAdItem from "./Items/NameImgAdItem";
import { ui } from "../../../../ui/layaMaxUI";
import { AdConfigData } from "./ShiyouAdManager";
import { BaseAdView } from "./BaseAdView";

export default class FriendPlayView extends ui.ShiyouAdView.FriendPlayViewUI implements BaseAdView {
    private m_hotsize = 52;
    private m_friednsize = 24;
    private m_hidecallBack = null;

    public SetHideCallBack(hidecallback) {
        this.m_hidecallBack = hidecallback;
    }

    public CancelHideCallBack() {
        this.m_hidecallBack = null;
    }

    constructor() {
        super();
        Laya.stage.addChild(this);
        this.m_FriendAdList.on(Laya.Event.MOUSE_DOWN, this, () => { Laya.timer.clearAll(this.m_FriendAdList); });
        this.m_FriendAdList.on(Laya.Event.MOUSE_UP, this, this.FriendListTweenToEnd);
        this.m_FriendAdList.on(Laya.Event.MOUSE_OUT, this, this.FriendListTweenToEnd);

        this.m_HotAdList.on(Laya.Event.MOUSE_DOWN, this, () => { Laya.timer.clearAll(this.m_HotAdList); });
        this.m_HotAdList.on(Laya.Event.MOUSE_UP, this, this.HotListTweenToEnd);
        this.m_HotAdList.on(Laya.Event.MOUSE_OUT, this, this.HotListTweenToEnd);
        this.m_CloseBtn.on(Laya.Event.CLICK, this, this.OnCloseBtnClick);
        this.visible = false;
    }

    createChildren() {
        super.createChildren();
        this.m_FriendAdList.renderHandler = new Laya.Handler(this, this.OnFriendListRenderHandler);
        this.m_FriendAdList.elasticEnabled = true;
        this.m_FriendAdList.selectEnable = true;
        this.m_FriendAdList.repeatX = 4;
        this.m_FriendAdList.hScrollBarSkin = "";

        this.m_HotAdList.renderHandler = new Laya.Handler(this, this.OnHotListRenderHandler);
        this.m_HotAdList.elasticEnabled = true;
        this.m_HotAdList.selectEnable = true;
        this.m_HotAdList.repeatX = 4;
        this.m_HotAdList.repeatY = 3;
        this.m_HotAdList.vScrollBarSkin = "";

    }

    public OnFriendListRenderHandler(cell: NameImgAdItem, index: number) {
        let skindata = cell.dataSource;
        cell.SetData(skindata,"Type4",skindata.Type4);
    }

    public OnHotListRenderHandler(cell: NameImgAdItem, index: number) {
        let skindata = cell.dataSource;
        cell.SetData(skindata,"Type4",skindata.Type4);
    }


    public InitFriendAdList(gameadsData) {
        if (gameadsData.length < 1) return;

        let addata = [];
        for (let index = 0; index < gameadsData.length; index++) {
            const element = gameadsData[index];
            if (element.AdType == 3) addata.push(element);
        }
        if (addata.length < 1) return;

        let banneraddata = [];
        if (addata.length < this.m_friednsize) {
            let index = 0;
            for (; index < addata.length;) {
                const element = addata[index];
                banneraddata.push(element);
                if (banneraddata.length >= this.m_friednsize)
                    break;
                if (index >= addata.length - 1)
                    index = 0;
                else index++;
            }
        }
        else {
            banneraddata = addata;
        }
        this.m_FriendAdList.array = banneraddata;
        this.FriendListTweenToEnd();
    }

    public InitHotPlayAdList(gameadsData: Array<AdConfigData>) {
        if (gameadsData.length < 1) return;

        let addata = [];
        for (let index = 0; index < gameadsData.length; index++) {
            const element = gameadsData[index];
            if (element.Type4 != null)
                addata.push(element);
        }
        if (addata.length < 1) return;
        let banneraddata = [];
        if (addata.length < this.m_hotsize) {
            let index = 0;
            for (; index < addata.length;) {
                const element = addata[index];
                banneraddata.push(element);
                if (banneraddata.length >= this.m_hotsize)
                    break;
                if (index >= addata.length - 1)
                    index = 0;
                else index++;
            }
        }
        else {
            banneraddata = addata;
        }
        this.m_HotAdList.array = banneraddata;
        this.HotListTweenToEnd();

    }


    InitData(data: Array<AdConfigData>) {
        this.InitFriendAdList(data);
        this.InitHotPlayAdList(data);
    }



    public Show() {
        this.visible = true;
    }

    public Hide() {
        this.visible = false;
        this.m_hidecallBack && this.m_hidecallBack();
    }



    public FriendListTweenToEnd() {
        Laya.timer.once(1000, this.m_FriendAdList, () => { this.m_FriendAdList.tweenTo(this.m_FriendAdList.length - 5, this.m_FriendAdList.length * 500, Laya.Handler.create(this, this.FriendListTweenToStart)); })

    }

    FriendListTweenToStart() {
        this.m_FriendAdList.tweenTo(0, this.m_FriendAdList.length * 500, Laya.Handler.create(this, this.FriendListTweenToEnd));
    }

    //热门推荐



    public HotListTweenToEnd() {
        Laya.timer.once(1000, this.m_HotAdList, () => { this.m_HotAdList.tweenTo(this.m_HotAdList.length - 9, this.m_HotAdList.length * 300, Laya.Handler.create(this, this.HotListTweenToStart)); });
    }

    HotListTweenToStart() {
        this.m_HotAdList.tweenTo(0, this.m_HotAdList.length * 300, Laya.Handler.create(this, this.HotListTweenToEnd));
    }


    OnCloseBtnClick() {
        this.Hide();
    }
}